public class Solution {
    public static void main(String[] args) {

    }

    public boolean isHappy(int n) {
        /**
         * 快乐数
         * 解题思路：定义快慢指针，其中快指针每次变换2次，慢指针每次变换一次
         * 当快慢指针相遇时，即说明他们在环中相遇，判断当前值即可
         * */
        int fast = n;
        int slow = n;
        while(true) {
            fast = happyNum(happyNum(fast));
            slow = happyNum(slow);
            if(fast == slow) {
                return fast == 1;
            }
        }

    }

    public static int happyNum(int num) {
        /**
         * 快乐数*/
        String n = String.valueOf(num);
        int total = 0;
        for(int i = 0; i < n.length(); i++) {
            total += Math.pow(n.charAt(i) - '0', 2);
        }
        return total;
    }
}
